package com.baizan.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.baizan.daka.base.BasePresenter
import com.baizan.daka.base.IView
import com.baizan.daka.ui.dialog.LoadingDialog
import com.classic.common.MultipleStatusView


/**
 * @author xuhao
 * created: 2017/10/25
 * desc:BaseActivity基类
 */
abstract class BaseActivity<P : BasePresenter> : AppCompatActivity(),
     IView {
    var mContext: Activity? = null
    var loading: LoadingDialog? = null

    /**
     * 多种状态的 View 的切换
     */
    protected var mLayoutStatusView: MultipleStatusView? = null
    var presenter: P? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId())
        presenter = initPresenter()
        mContext = this
        initData()
        initView()
        start()
        initListener()


    }


    override fun showLoadingDialog() {
        loading ?: let {
            loading = LoadingDialog(it)
        }

        loading?.let {
            it.setDialogLabel("加载中...")
            if (!it.isShowing()) {
                it.show()
            }
        }

    }

    open fun showLoadingDialog(label: String) {
        loading ?: let {
            loading = LoadingDialog(it)
        }
        loading?.let {
            it.setDialogLabel(label)
            if (!it.isShowing()) {
                it.show()
            }
        }
    }

    override fun hideLoadingDialog() {
        try {
            loading?.let {
                if (it.isShowing()) {
                    it.dismiss()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    abstract fun initPresenter(): P?

    private fun initListener() {
        mLayoutStatusView?.setOnClickListener(mRetryClickListener)
    }

    open val mRetryClickListener: View.OnClickListener = View.OnClickListener {
        start()
    }


    /**
     *  加载布局
     */
    abstract fun layoutId(): Int

    /**
     * 初始化数据
     */
    abstract fun initData()

    /**
     * 初始化 View
     */
    abstract fun initView()

    /**
     * 开始请求
     */
    abstract fun start()


    /**
     * 打卡软键盘
     */
    fun openKeyBord(mEditText: EditText, mContext: Context) {
        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN)
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
    }

    /**
     * 关闭软键盘
     */
    fun closeKeyBord(mEditText: EditText, mContext: Context) {
        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(mEditText.windowToken, 0)
    }


    override fun onDestroy() {
        super.onDestroy()
        presenter?.detachView()
//        MyApplication.getRefWatcher(this)?.watch(this)
    }



}


